﻿using System;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Collections.Generic;

namespace PasswordProvider.ObjectModel
{
    /// <summary>
    /// Base class for items to be listed in the collection.
    /// </summary>    
    [KnownType(typeof(LogOnSite))]
    [KnownType(typeof(CreditCardSite))]
    [KnownType(typeof(GenericSite))]
    [DataContract]
    public abstract class Site : DataObject
    {
        protected Site() { }
        protected Site(string name, string description)
        {
            _name = name;
            _description = description;
        }

        public override string ToString()
        {
            return Name;
        }

        #region Public Properties
        [DataMember]
        public Guid Guid
        {
            get 
            {
                if (_guid == Guid.Empty) 
                    _guid = Guid.NewGuid();
                return _guid; 
            }
            set { _guid = value; }
        } private Guid _guid;
        
        [DataMember]
        public string Name
        {
            get { return _name; }
            set { SetPropertyValue(value, ref _name, "Name"); }
        } private string _name; 
        
        [DataMember]
        public string Description
        {
            get { return _description; }
            set { SetPropertyValue(value, ref _description, "Description"); }
        } private string _description;

        [DataMember]
        public ObservableCollection<string> Tags
        {
            get
            {
                if (_tags == null)
                {
                    Tags = new ObservableCollection<string>();
                }
                return _tags;
            }
            private set
            {
                _tags = value;
                _tags.CollectionChanged += delegate { OnPropertyChanged("Tags"); };

            }        
        } private ObservableCollection<string> _tags;
        
        #endregion

        #region Abstract and Virtual

        public abstract EncryptedValue PrimaryEncryptedValue { get; }
        internal abstract string WriteExportString(SecurityManager securityManager, bool decryptAll);
        internal abstract Site Clone();

        internal virtual void Copy(Site site)
        {
            Guid = site.Guid;
            Name = site.Name;
            Description = site.Description;
            
            _tags = site.Tags;            
        }

        #endregion

    }
}
